#include "fltkwindows.h"
#include <FL/Fl.H>
#include<Poco/Timestamp.h>
#include<Poco/DateTimeParser.h>
#include "Poco/DateTimeFormatter.h"


void newstextwindow::timer(void* newstext)
{
    ((newstextwindow*) newstext)->update();
    int random_integer;
    random_integer = (rand()%15)+20;
    Fl::repeat_timeout(random_integer,timer,newstext);
}

void newstextwindow::update()
{
    CL_News n = newspool->old_news_first();
    textbuffer.text(n.newsitem.description.c_str());
    textbuffer.append("\n\n");
    Poco::Timestamp ts=Poco::Timestamp::fromEpochTime(n.newsitem.pubDate);
    Poco::DateTime dateTime(ts);
    std::string date=Poco::DateTimeFormatter::format(dateTime, "%e %b %Y %H:%M");
    if(n.channel.titel!="")
    {
        copy_label((n.channel.titel+" "+date).c_str());
    }
    else
    {
        copy_label((n.channel.description+" "+date).c_str());
    }
    textbuffer.append(n.channel.copyright.c_str());
    parent()->redraw();
}

newstextwindow::newstextwindow(int x, int y, int w, int h):Fl_Text_Display(x,y,w,h,"News"),
    newspool(CL_News_pool::get_news_pool_pointer()),textbuffer()
{
    wrap_mode(WRAP_AT_BOUNDS,0);
    scrollbar_width(0);
    labelsize(12);
    resize(x,y+labelsize()*4,w,h-labelsize()*4);
    buffer(textbuffer);
    update();
    Fl::add_timeout(20,timer,this);
}

void newsgroupwindow::timer(void* newsgroup)
{
    ((newsgroupwindow*) newsgroup)->update();
    int random_integer;
    random_integer = (rand()%15)+20;
    Fl::repeat_timeout(random_integer,timer,newsgroup);
}

void newsgroupwindow::update()
{
}

newsgroupwindow::newsgroupwindow(int x, int y, int w, int h):Fl_Group(x,y,w,h)
{
    newstextwindow* ntw=new newstextwindow(x,y,w,h);
    end();
}

screensaverwindow::screensaverwindow(int argc, char* argv[]):Fl_Window(300, 180, "This is me")
{
    fullscreen();
    int news_height=(Fl::h()*4/15);
    int news_width=(Fl::w()-20);

    newstextwindow* newswindows[3];
    for(int i=0; i<3; i++)
    {
        newswindows[i]=new newstextwindow(10,i*news_height,news_width,news_height);
        newswindows[i]->box(FL_UP_BOX);
        newswindows[i]->labelsize(36);
        newswindows[i]->labelfont (FL_BOLD+FL_ITALIC);
        newswindows[i]->labeltype (FL_SHADOW_LABEL);
    }
    end ();
    show (argc, argv);

};

